package com.ruoyi.transaction.service.impl;


import com.ruoyi.common.entity.TransactionEntity.VO.SpuWithPrice;
import com.ruoyi.transaction.service.ISpuWithPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * SPU带价格Service实现
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class SpuWithPriceServiceImpl implements ISpuWithPriceService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<SpuWithPrice> selectSpuWithPriceByRange(Long startId, Long endId) {
        String sql = "SELECT " +
                "s.id, " +
                "s.category_id, " +
                "s.name, " +
                "s.brand_name, " +
                "s.unit, " +
                "s.description, " +
                "s.image_urls, " +
                "s.barcode, " +
                "s.is_active, " +
                "s.del_flag, " +
                "s.create_time, " +
                "s.update_time, " +
                "MIN(sk.price) as min_price " +
                "FROM spu s " +
                "LEFT JOIN sku sk ON s.id = sk.spu_id AND sk.is_active = '1' AND sk.del_flag = '0' " +
                "WHERE s.id BETWEEN ? AND ? " +
                "AND s.del_flag = '0' " +
                "GROUP BY s.id, s.category_id, s.name, s.brand_name, s.unit, s.description, " +
                "s.image_urls, s.barcode, s.is_active, s.del_flag, s.create_time, s.update_time " +
                "ORDER BY s.id";

        return jdbcTemplate.query(sql, new SpuWithPriceRowMapper(), startId, endId);
    }

//    @Override
//    public List<SpuWithPrice> selectSpuWithPriceByIds(List<Long> ids) {
//        if (ids == null || ids.isEmpty()) {
//            return List.of();
//        }
//
//        // 构建IN查询的占位符
//        String placeholders = String.join(",", ids.stream().map(id -> "?").toList());
//
//        String sql = "SELECT " +
//                "s.id, " +
//                "s.category_id, " +
//                "s.name, " +
//                "s.brand_name, " +
//                "s.unit, " +
//                "s.description, " +
//                "s.image_urls, " +
//                "s.barcode, " +
//                "s.is_active, " +
//                "s.del_flag, " +
//                "s.create_time, " +
//                "s.update_time, " +
//                "MIN(sk.price) as min_price " +
//                "FROM spu s " +
//                "LEFT JOIN sku sk ON s.id = sk.spu_id AND sk.is_active = '1' AND sk.del_flag = '0' " +
//                "WHERE s.id IN (" + placeholders + ") " +
//                "AND s.del_flag = '0' " +
//                "GROUP BY s.id, s.category_id, s.name, s.brand_name, s.unit, s.description, " +
//                "s.image_urls, s.barcode, s.is_active, s.del_flag, s.create_time, s.update_time " +
//                "ORDER BY s.id";
//
//        return jdbcTemplate.query(sql, new SpuWithPriceRowMapper(), ids.toArray());
//    }

    /**
     * SPU带价格行映射器
     */
    private static class SpuWithPriceRowMapper implements RowMapper<SpuWithPrice> {
        @Override
        public SpuWithPrice mapRow(ResultSet rs, int rowNum) throws SQLException {
            SpuWithPrice spuWithPrice = new SpuWithPrice();

            spuWithPrice.setId(rs.getLong("id"));
            spuWithPrice.setCategoryId(rs.getLong("category_id"));
            spuWithPrice.setName(rs.getString("name"));
            spuWithPrice.setBrandName(rs.getString("brand_name"));
            spuWithPrice.setUnit(rs.getString("unit"));
            spuWithPrice.setDescription(rs.getString("description"));
            spuWithPrice.setImageUrls(rs.getString("image_urls"));
            spuWithPrice.setBarcode(rs.getString("barcode"));
            spuWithPrice.setIsActive(rs.getString("is_active"));
            spuWithPrice.setDelFlag(rs.getString("del_flag"));
            spuWithPrice.setCreateTime(rs.getTimestamp("create_time"));
            spuWithPrice.setUpdateTime(rs.getTimestamp("update_time"));

            // 处理最低价格，如果没有SKU则设为null
            BigDecimal minPrice = rs.getBigDecimal("min_price");
            spuWithPrice.setPrice(minPrice);

            return spuWithPrice;
        }
    }
}